<?php
class Affiliate extends Model {

	private $affiliate_id;
	private $firstname;
	private $lastname;
	private $email;
	private $telephone;
	private $fax;
	private $code;
	
  	public function __construct($registry) {
        parent::__construct($registry);

		$this->request = $registry->get('request');
		$this->session = $registry->get('session');
				
		if (isset($this->session->data['affiliate_id'])) { 
            $affiliate_query =  $this->pdo->prepare("SELECT * 
                FROM " . $this->db_prefix . "affiliate 
                WHERE affiliate_id = :affiliate_id 
                AND status = '1'"); 

            $affiliate_query->execute(array(
                ':affiliate_id' => $this->session->data['affiliate_id'] 
            ));
			
			if ($affiliate_query->rowCount()) {
                $result = $affiliate_query->fetch(PDO::FETCH_ASSOC);
				$this->affiliate_id = $result['affiliate_id'];
				$this->firstname = $result['firstname'];
				$this->lastname = $result['lastname'];
				$this->email = $result['email'];
				$this->telephone = $result['telephone'];
				$this->fax = $result['fax'];
				$this->code = $result['code'];

                $update_query = $this->pdo->prepare("UPDATE " . $this->db_prefix . "affiliate 
                    SET ip = :ip 
                    WHERE affiliate_id = :affiliate_id");
						
                $update_query->execute(array(
                    ':ip'           => $this->request->server['REMOTE_ADDR'],
                    ':affiliate_id' => $this->session->data['affiliate_id']
                ));

			} else {
				$this->logout();
			}
  		}
	}
		
  	public function login($email, $password) {

        $affiliate_query = $this->pdo->prepare("SELECT * 
            FROM " . $this->db_prefix . "affiliate 
            WHERE LOWER(email) = :email 
            AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1(:password)))))) 
            OR password = :password_md5 
            AND status = '1' 
            AND approved = '1'");

        $affiliate_query->execute(array(
            ':email'    => mb_strtolower($email),
            ':password' => $password,
            ':password_md5' => md5($password)
        ));
		
		if ($affiliate_query->rowCount()) {

            $result = $affiliate_query->fetch(PDO::FETCH_ASSOC);
			$this->session->data['affiliate_id'] = $result['affiliate_id'];	
		    
			$this->affiliate_id = $result['affiliate_id'];
			$this->firstname = $result['firstname'];
			$this->lastname = $result['lastname'];
			$this->email = $result['email'];
			$this->telephone = $result['telephone'];
			$this->fax = $result['fax'];
      		$this->code = $result['code'];
	  
	  		return true;
    	} else {
      		return false;
    	}
  	}
  
  	public function logout() {
		unset($this->session->data['affiliate_id']);

		$this->affiliate_id = '';
		$this->firstname = '';
		$this->lastname = '';
		$this->email = '';
		$this->telephone = '';
		$this->fax = '';
  	}
  
  	public function isLogged() {
    	return $this->affiliate_id;
  	}

  	public function get_id() {
    	return $this->affiliate_id;
  	}
      
  	public function get_firstname() {
		return $this->firstname;
  	}
  
  	public function get_lastname() {
		return $this->lastname;
  	}
  
  	public function get_email() {
		return $this->email;
  	}
  
  	public function getTelephone() {
		return $this->telephone;
  	}
  
  	public function getFax() {
		return $this->fax;
  	}
	
  	public function getCode() {
		return $this->code;
  	}	
}
?>
